<?php
namespace Admin\Controller;
use Think\Controller;
class AdminController extends Controller
{
    public function add()
    {
    	if(IS_POST)
    	{

            // var_dump(I('post.'));die;
    		$model = D('Admin');
    		if($model->create(I('post.'), 1))
    		{
    			if($id = $model->add())
    			{
    				$this->success('添加成功！', U('lst?p='.I('get.p')));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}
        $pmodel = D('project');
        $project = $pmodel->select();
        $this->assign('project', $project);
		// 设置页面中的信息
		$this->assign(array(
			'_page_title' => '添加',
			'_page_btn_name' => '列表',
			'_page_btn_link' => U('lst'),
		));
		$this->display();
    }
    public function edit()
    {
    	$id = I('get.id');
        // /echo $id;die;
    	if(IS_POST)
    	{
    		$model = D('Admin');
    		if($model->create(I('post.'), 2))
    		{
    			if($model->save() !== FALSE)
    			{
    				$this->success('修改成功！', U('lst', array('p' => I('get.p', 1))));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}
    	$model = M('Admin');
    	$data = $model->find($id);
        //var_dump($data);die;
    	$this->assign('data', $data);
        $pmodel = D('project');
        $project = $pmodel->select();
        $this->assign('project', $project);
		// 设置页面中的信息
		$this->assign(array(
			'_page_title' => '修改',
			'_page_btn_name' => '列表',
			'_page_btn_link' => U('lst'),
		));
		$this->display();
    }
    public function delete()
    {
    	$model = D('Admin');
    	if($model->delete(I('get.id', 0)) !== FALSE)
    	{
    		$this->success('删除成功！', U('lst', array('p' => I('get.p', 1))));
    		exit;
    	}
    	else
    	{
    		$this->error($model->getError());
    	}
    }
    public function lst()
    {

        if(session('id')==1){
            $model = D('Admin');
            $data = $model->search();
            $this->assign(array(
            'data' => $data['data'],
            'page' => $data['page'],
        ));
        }else{
            $model = D('Admin');
            $id=session('id');
            $data = $model->where(array('id'=>array('eq',$id),))->select();
            //var_dump($data);die;
            $this->assign(array(
            'data' => $data,
            ));
        }
        $pmodel = D('project');
        $project = $pmodel->select();
        $this->assign('project', $project);
		// 设置页面中的信息
		$this->assign(array(
			'_page_title' => '列表',
			'_page_btn_name' => '添加',
			'_page_btn_link' => U('add'),
		));
    	$this->display();
    }
    public function setStatus(){
        $model = D('admin');
        $id  = I('get.id');
        // var_dump($id);
        $status = I('get.s');
        //var_dump($status);die;
        if($status == 0){
           $data['status']=1;
            $result=$model->where('id='.$id)->save($data);
            if($result)
            {
               $this->success('启用成功',U('admin/lst'));
            }
        }
        else{
           $data['status']=0;
            $result=$model->where('id='.$id)->save($data);
            if($result)
            {
                $this->success('停用成功',U('admin/lst'));
            }
        }
    }
}
